<template>
  <div>
    <nut-config-provider
      :theme-vars="{
        navbarTitleFont: '32rpx',
        navbarTitleFontWeight: '500',
        navbarTitleFontColor: '#000',
        navbarBoxShadow: 'none',
        navbarColor: '#000',
        navbarBackground: '#fff',
        checkboxMarginRight: '0'
      }">
      <uNavbar :title="functionName" :is-back="false" fixed :custom-style="{ background: '#fff', zIndex: 999 }">
        <template v-if="!curPageIsHaveTab" #icon>
          <nut-icon name="home"></nut-icon>
        </template>
      </uNavbar>
      <index ref="indexRef" :mall="true" />
    </nut-config-provider>
    <!-- 优惠券活动弹窗组件 -->
    <tpl_coupon_activity ref="couponRef" :res="{ data: {} }" />
  </div>
  <tabbar v-if="curPageIsHaveTab"></tabbar>
  <shop-tabbar v-else></shop-tabbar>
</template>

<script setup lang="ts">
import { onLoad, onShow, onPullDownRefresh } from '@dcloudio/uni-app'
import { isLogin } from '@/utils/filters'
import { useTabbarStore } from '@/store'
const tabbarStore = useTabbarStore()
const tabbarList = tabbarStore.tabbarList.map((i) => i.linkUrl)
import index from '@/pages/index.vue'
import uNavbar from '@/components/navbar/index.vue'
import { getCouponActivity } from '@/api/promotion'
import tpl_coupon_activity from '@/pages/v3-template/tpl_coupon_activity.vue' // 优惠券

const indexRef = ref<any>('')
const id = ref('')
const functionName = ref('商城')
const pages = getCurrentPages() // 获取当前页面栈
const currentPage = pages[pages.length - 1] // 当前页面
const currentPath = `/${currentPage.route}` // 获取当前页面路径
const curPageIsHaveTab = computed(() => {
  return tabbarList.includes(currentPath)
})

const couponRef = ref<any>(null) // 新增优惠券组件引用

// 获取优惠券活动并展示
async function checkCouponActivity() {
  try {
    // 获取上次请求时间
    const lastRequestTime = uni.getStorageSync('couponActivityLastRequestTime')
    const currentTime = new Date().getTime()

    // 如果是首次访问或者距离上次请求时间超过24小时
    // if (!lastRequestTime || (currentTime - lastRequestTime) > 24 * 60 * 60 * 1000) {
      const res = await getCouponActivity()
      if (res.data.success && res.data.result) {
        // 保存请求时间
        uni.setStorageSync('couponActivityLastRequestTime', currentTime)

        // 延迟确保组件已渲染
        setTimeout(() => {
          // 显示优惠券弹窗
          if (couponRef.value) {
            couponRef.value.showCouponPopup(res.data.result)
          }
        }, 500)
      }
    // }
  }
  catch (error) {
    console.error('获取优惠券活动失败', error)
  }
}

onPullDownRefresh(() => {
  indexRef.value?.init()
  setTimeout(() => {
    uni.stopPullDownRefresh()
  }, 1000)
})

onLoad((options: any) => {
  id.value = options.id
  if (tabbarStore.activeTabName && curPageIsHaveTab.value) {
    functionName.value = tabbarStore.activeTabName
  }
  if (options.functionName) {
    // uni.setNavigationBarTitle({
    //   title: options.functionName
    // })
    functionName.value = options.functionName
  }
})
onShow(async () => {
  if (isLogin('auth')) {
    // 检查并获取优惠券活动
    checkCouponActivity()
  }
})
</script>

<style scoped></style>
